<?
include_once dirname(__FILE__).'/UploadFile.class.php';
//IncludeClass(UploadFile,App::$PATH_LIBPHP);

define('UPPHOTO_ERR_NOTIMG',11) ;					// 不是有效图像类型
define('UPPHOTO_ERR_EXWIDTH',12) ;					// 图像宽度超过限制
define('UPPHOTO_ERR_EXHEIGHT',13) ;					// 图像高度超过限制

class UploadPhoto extends UploadFile
{
	function UploadPhoto($strAllowExt=_define_by_App_class_,$nMaxByte=_define_by_App_class_,$strStoreDir=_define_by_App_class_,$nMaxWidth=_define_by_App_class_,$nMaxHeight=_define_by_App_class_)
	{
		if( $strAllowExt==_define_by_App_class_ )
			$strAllowExt = App::$UPPHOTO_ALLOWEXT ;

		$this->UploadFile($strAllowExt,$nMaxByte,$strStoreDir) ;
		
		$this->nMaxWidth = ($nMaxWidth==_define_by_App_class_)? App::$UPPHOTO_MAXWIDTH: $nMaxWidth ;
		$this->nMaxHeight = ($nMaxHeight==_define_by_App_class_)? App::$UPPHOTO_MAXHEIGHT: $nMaxHeight ;
	}
	
	protected $nMaxWidth ;	
	protected $nMaxHeight ;	
	
	protected $arrImageInfo ;
	
	public function Upload($InputName)
	{
		// 上传
		if( !UploadFile::Upload($InputName) )
			return false ;
		
		// 获得 图像信息
		$this->arrImageInfo[$InputName] = getimagesize( $this->GetStorePath($InputName) ) ;
		if($this->arrImageInfo[$InputName]===false)
		{
			$this->CancelUpload() ;
			$this->_PutErrorInfo($InputName,'上传的文件不是有效的图片类型：“'.$this->GetOriginalName($InputName).'”',UPPHOTO_ERR_NOTIMG) ;
			return false ;
		}

		// 检查 图像宽度
		$nWidth = $this->GetPhotoWidth() ;
		if( $this->nMaxWidth>0 and $this->nMaxWidth<$nWidth )
		{
			$this->CancelUpload() ;
			$this->_PutErrorInfo($InputName,"图片宽度（{$nWidth} px）超过限制（{$this->nMaxWidth} px）",UPPHOTO_ERR_EXWIDTH) ;
			return false ;
		}

		// 检查 图像高度
		$nHeight = $this->GetPhotoHeight() ;
		if( $this->nMaxHeight>0 and $this->nMaxHeight<$nHeight )
		{
			$this->CancelUpload() ;
			$this->_PutErrorInfo($InputName,"图片高度（{$nHeight} px）超过限制（{$this->nMaxHeight} px）",UPPHOTO_ERR_EXHEIGHT) ;
			return false ;
		}
		
		return true ;
	}
	
	public function GetPhotoWidth($Input=_last_upload_input_)
	{ return $this->arrImageInfo[ ($Input==_last_upload_input_)?$this->strLastInput:$Input ][0] ; }
	
	public function GetPhotoHeight($Input=_last_upload_input_)
	{ return $this->arrImageInfo[ ($Input==_last_upload_input_)?$this->strLastInput:$Input ][1] ; }
	
	public function GetPhotoType($Input=_last_upload_input_)
	{ return $this->arrImageInfo[ ($Input==_last_upload_input_)?$this->strLastInput:$Input ][2] ; }
	
	public function SetMaxSize($nMaxWidth=0,$nMaxHeight=0)
	{
		$this->nMaxWidth = $nMaxWidth ;
		$this->nMaxHeight = $nMaxHeight ;
	}
	
	public function GetMaxWidth()
	{ return $this->nMaxWidth ; }
	
	public function GetMaxHeight()
	{ return $this->nMaxHeight ; }

}

?>